package cn.edu.gdpt.mobile0123456789hwh0402;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
//1、全局控件变量声明
    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//3、调用控件初始化方法initView()
//       6、三个checkbox控件绑定监听器对象
        chkboxMainYinqing.setOnCheckedChangeListener(this);
        chkboxMainBoli.setOnCheckedChangeListener(this);
        chkboxMainFangxiangpan.setOnCheckedChangeListener(this);
//        7、给按钮控件添加点击事件监听器
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                8、在点击处理单元中，依次对复选框进行判断，如果选中的，获取控件文本内容
                String msg="选中：";
                if (chkboxMainYinqing.isChecked()){
                    msg+="\n"+chkboxMainYinqing.getText().toString();
                }
                if(chkboxMainBoli.isChecked()){
                    msg+="\n"+chkboxMainBoli.getText().toString();
                }
                if(chkboxMainFangxiangpan.isChecked()){
                    msg+="\n"+chkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
    }
//2、定义控件初始化方法initView,调用findViewById()方法，获取界面控件
    private void initView() {
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkboxMainYinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkboxMainBoli);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.chkboxMainFangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btnMainBuy);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }
//    4、实现CompoundButton.OnCheckedChangeListener接口，重写onCheckedChanged()方法
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//        5、实现选项选中状态信息显示
        if(b){
            tvMainResult.setText("选中："+compoundButton.getText().toString());
        }else {
            tvMainResult.setText("取消选中："+compoundButton.getText().toString());
        }

    }
}